package com.cn.wanxi.consumer.listener;

import com.cn.wanxi.consumer.domain.entity.ThreadMsg;
import com.cn.wanxi.consumer.domain.entity.User;
import com.cn.wanxi.consumer.domain.model.ResultModel;
import com.cn.wanxi.consumer.service.IThreadMsgService;
import com.cn.wanxi.consumer.util.ParamUtil;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @ClassName ThreadListener
 * @Description 线程消息的监听器
 * @Author JiJiang
 * @Date 2022/9/29 13:48
 * @Version 1.0
 */
@Component
public class ThreadListener {


    private int receiveCount = 0;
    @Autowired
    private IThreadMsgService threadMsgService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = ParamUtil.QUEUE_NAME_THREAD) //指定该监听器绑定的队列名
            , exchange = @Exchange(name = ParamUtil.EXCHANGE_NAME_THREAD, type = ExchangeTypes.TOPIC)
            , key = {ParamUtil.BINDING_KEY_NAME_THREAD}
    ))
    public void ThreadMsgQueReceive(ThreadMsg threadMsg) {
        //设置接收时间
        threadMsg.setReceiveTime(System.currentTimeMillis());

        //将消息保存到数据库
        ResultModel resultModel = threadMsgService.saveTheadMsg(threadMsg);
        if (resultModel.getCount() > 0) {
            System.out.println("成功接收" + (++receiveCount) + "条消息");
        }

    }

}
